from selenium import webdriver
import time
# 调用键盘按键操作需要引入keys包
from selenium.webdriver.common.keys import Keys

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome(options=chrome_options)
driver.set_window_size(1366, 768)
# get方法会一直等到页面加载，然后才会继续程序，通常测试会在这里选择time.sleep(2)
driver.get("http://www.baidu.com/")
# id="kw"是百度搜索输入框，输入字符串"情人节"
driver.find_element_by_id('kw').send_keys(u'情人节')
# ctrl+a全选输入框内容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'a')
# ctrl+x剪切输入框内容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'x')
# 输入框重新输入内容
driver.find_element_by_id('kw').send_keys('鲜花')
# 模拟Enter回车键
driver.find_element_by_id('su').send_keys(Keys.RETURN)
time.sleep(5)
# 清空输入框内容
driver.find_element_by_id('kw').clear()
# 生成新的页面快照
driver.save_screenshot(r'D:\test\鲜花.png')
# 获取当前url
print(driver.current_url)
driver.quit()